<?php
namespace app\controller;

use app\BaseController;
use app\validate\AdminUserValidate;
use common\models\AdminUserModel;
use common\utils\Helper;
use think\facade\Cookie;
use think\facade\Session;
use think\response\Redirect;

class Index extends AdminBase
{
    public function index()
    {
        return view('index', [
            'username' => 'August',
            'created_at' => date('Y-m-d H:i:s')
        ]);
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }

    public function login()
    {
        if ($this->request->isPost()) {
            $postData = array_map('trim', $_POST);
            $model = AdminUserModel::where('username', $postData['username'])->find();
            if (!$model || $model['status'] != STATUS_ACTIVE) {
                echo "用户不存在或者被禁止登陆!";
            } elseif (!password_verify($postData['password'], $model['password'])) {
                echo "用户名或密码不正确";
            } else {
                // 登陆，保存session, 如果勾选记住我，7天内直接登陆
               Session::set('user.id', $model->id);
               Session::set('user.username', $model->username);
               if (!empty($postData['remember_me'])) {
                   Cookie::set('username', $model->username, 7 * 86400);
                    Cookie::set('remember_me', substr(md5('$'.$model->username.mt_rand(1, 999)), 8, 16), 7 * 86400);
               }
               return redirect('index');
            }
        }

        return \think\facade\View::fetch('login', [
            'username' => $_POST['username'] ?? '',
            'password' => $_POST['password'] ?? '',
        ]);
    }

    public function register()
    {
        if ($this->request->isPost()) {
            try {
                $postData = array_map('trim', $_POST);
                $this->validate($postData, AdminUserValidate::class.'.register');

                $data = [
                    'username' => $postData['username'],
                    'password' => password_hash($postData['password'], PASSWORD_DEFAULT),
                    'email' => $postData['email'],
                    'status' => STATUS_ACTIVE,
                ];
                $model = new AdminUserModel();
                if (!$model->save($data)) {
                    echo "保存出错，请联系站长！";
                } else {
                    return \redirect('index/index');
                }
            } catch(\Exception $e) {
                echo $e->getMessage();
            }
        }

        return \think\facade\View::fetch('register', [
            'username' => $_POST['username'] ?? '',
            'email' => $_POST['email'] ?? '',
        ]);
    }

    public function logout()
    {
        Cookie::delete('username');
        Cookie::delete('remember_me');
        Session::delete('user.id');
        Session::delete('user.username');
        Session::destroy();
        return redirect('index/login');
    }
}
